package com.ycfmedical.bed.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.ycfmedical.bed.Constants;
import com.ycfmedical.bed.ControlBedConstants;
import com.ycfmedical.bed.R;
import com.ycfmedical.bed.factory.OrderFactory;
import com.ycfmedical.bed.local.YcfDbHelper;
import com.ycfmedical.bed.local.dao.HisSystemInfoDao;
import com.ycfmedical.bed.local.dao.PhysicianChargeDAO;
import com.ycfmedical.bed.local.dao.StaffDAO;
import com.ycfmedical.bed.local.dao.UserDao;
import com.ycfmedical.bed.service.VitalSignService;
import com.ycfmedical.bed.wifi.WifiEvent;


import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.NetProvider;
import cn.droidlover.xdroidmvp.net.RequestHandler;
import cn.droidlover.xdroidmvp.net.XApi;
import cn.droidlover.xdroidmvp.router.Router;
import okhttp3.CookieJar;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;

/**
 * 过渡页面 Created by Egoist on 2017-04-05.
 */

public class LoadingActivity extends AppCompatActivity
{
    //WIFI操作所需变量
    WifiEvent wifi = WifiEvent.getInstance();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loading);
        initData();
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();
        //页面跳转
        _initLoading();
    }

    private void initData()
    {
        _initLoading();
        _initDBase();
        _initNetProvider();
        _initVsSerice();
        wifi.getconnect(Constants.wifiIpAddressed,Constants.comPort);
//        wifi.senddata(OrderFactory.getMessage(ControlBedConstants.REQUEST_ZGB_MACADDRESS));
    }

    private void _initVsSerice()
    {
        Intent intent = new Intent(this, VitalSignService.class);
        startService(intent);
    }

    private void _initLoading()
    {
        new Handler().postDelayed(new Runnable()
        {
            @Override
            public void run()
            {
                SplashActivity.launch(LoadingActivity.this);
            }
        }, 1000);
    }

    /**
     * 初始化数据库
     */
    private void _initDBase()
    {
        // 通过 DaoMaster 的内部类 DevOpenHelper，你可以得到一个便利的 SQLiteOpenHelper 对象。
        // 注意：默认的 DaoMaster.DevOpenHelper 会在数据库升级时，删除所有的表，意味着这将导致数据的丢失。
        // 所以，在正式的项目中，还应该做一层封装，来实现数据库的安全升级。
        YcfDbHelper.init(this);
        // 向数据库添加数据
        UserDao.updateLocalData(this,YcfDbHelper.getDaoSession(this));
        HisSystemInfoDao.updateLocalData(this,YcfDbHelper.getDaoSession(this));
        StaffDAO.updateLocalData(this, YcfDbHelper.getDaoSession(this));
        PhysicianChargeDAO.updatePhysicianChargeData(this,YcfDbHelper.getDaoSession(this));
    }

    /**
     * 初始化网络库
     */
    private void _initNetProvider() {
        XApi.registerProvider(new NetProvider() {
            @Override
            public Interceptor[] configInterceptors() {
                return new Interceptor[0];
            }

            @Override
            public void configHttps(OkHttpClient.Builder builder) {

            }

            @Override
            public CookieJar configCookie() {
                return null;
            }

            @Override
            public RequestHandler configHandler() {
                return null;
            }

            @Override
            public long configConnectTimeoutMills() {
                return 0;
            }

            @Override
            public long configReadTimeoutMills() {
                return 0;
            }

            @Override
            public boolean configLogEnable() {
                return true;
            }

            @Override
            public boolean handleError(NetError error) {
                return false;
            }
        });
    }

    public static void launch(AppCompatActivity activity)
    {
        Router.newIntent(activity).to(LoadingActivity.class).launch();
    }

}
